package gui;

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import main.*;

public class Fenetre extends JFrame implements ActionListener, KeyListener{
	
	private ChatGUI chatGUI;
	private JTextField username;
	private JButton connect;
	private JPanel mainPanel;
	
	public Fenetre(ChatGUI chatGUI) {
		
			this.chatGUI = chatGUI;
			
			this.setSize(1000, 600);
			this.setResizable(false);
		    
			
		    // main panel
		    
		    this.mainPanel = new JPanel();
		    mainPanel.setLayout(null);
		    mainPanel.setBackground(new Color(255, 255, 255));
		    Insets mainInsets = mainPanel.getInsets();
		    
		    // subContent panel
		    
		    JPanel subContent = new JPanel();
		    subContent.setLayout(null);
		    Insets insets = subContent.getInsets();
		    
		    subContent.setBounds(400 + mainInsets.left, 200 + mainInsets.top, 200, 200);
		    subContent.setBackground(new Color(255, 255, 255));
		    
		    mainPanel.add(subContent);
		    
		    // logo
		    
		    Icone logo = new Icone();
		    logo.setBounds(mainInsets.left + 380, mainInsets.top + 82, 250, 200);
		    
		    mainPanel.add(logo);
		    
		    // text field
		    
		    this.username = new JTextField();
		    username.setBounds(insets.left, 100 + insets.top, 200, 40);
		    
		    subContent.add(username);
		    
		    // connect button
		    
		    this.connect = new JButton("connect");
		    connect.setBounds(insets.left, 150 + insets.top, 200, 50);
		    
		    subContent.add(connect);
		    
		    // display mainPanel in the window
		    
		    this.setContentPane(mainPanel);
		    
		    //******************************************************************************
		    
		    // Listeners
		    
		    connect.addActionListener(this);
		    username.addKeyListener(this);
		    
	}

	public void actionPerformed(ActionEvent e)
    {
		if(e.getSource() == connect){
			this.chatGUI.getController().getCurrentState().connect(this.username.getText());
			ConnectingView nextView = new ConnectingView();
			this.setContentPane(nextView);
			
		}
    }
	
	public void keyPressed(KeyEvent e)
    {
		if(e.getKeyCode() == KeyEvent.VK_ENTER){
			this.chatGUI.getController().getCurrentState().connect(this.username.getText());
			ConnectingView nextView = new ConnectingView();
			this.mainPanel.setVisible(false);
			nextView.setVisible(true);
			this.setContentPane(nextView);
			
		}
    }

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}  
		
}
